<?php
class CajasController extends AppController {

	var $name = 'Cajas';
    var $uses = array('Caja','User','UnidadesTributaria');
    var $components = array('Acl', 'Auth', 'Session');
	//var $paginate = array('limit' => 20, 'order' => array( 'Caja.created' => 'asc'));
    var $paginate = array(
    'Caja' => array ('limit' => 20, 'order' => array( 'Caja.created' => 'asc')),
    'Author' => array ('limit' => 10, 'order' => array( 'Caja.created' => 'asc'))
);
	function index() {
		$data = $this->paginate('Caja', array('Caja.abierta' => '1'));
        $this->set(compact( 'data'));
		//$this->set('cajas', $this->Caja->find('list', array('conditions' => array('abierta' => '1'))));
	}


    function apertura($id = null){
        
            $id = $this->Caja->find('count')+1;
            //$this->Caja->read(null, $id);
            $this->Caja->create();
            $this->data['Caja']['id'] = $id;
            $this->data['Caja']['user_id'] = $this->Auth->user('id');
            $this->data['Caja']['abierta'] = 1;
            $this->data['Caja']['transacciones'] = 0;
            $this->data['Caja']['recaudacione_id'] = 1;
            $this->data['Caja']['monto'] = 0;
            //$this->Caja->save($this->data);
            print_r($this->data);
            
            //$caja->save();
            if ($this->Caja->save($this->data))
                {
                    $this->Session->setFlash(__('Caja creada', true));
                    $this->User->read(null, $this->data['Caja']['user_id']);
                    $this->User->saveField('activo', "1");
                }
            $this->redirect(array('controller' => 'users', 'action' => 'perfil'));
    }
    function historial() {
		$cajas = $this->paginate('Caja', array('Caja.abierta' => '0'));
        $this->set(compact( 'cajas'));
		//$this->set('cajas', $this->Caja->find('list', array('conditions' => array('abierta' => '1'))));
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid caja', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('caja', $this->Caja->read(null, $id));
	}

	function add() {
        print_r($this->data);
		if (!empty($this->data)) {
			$this->Caja->create();
            $this->data['Caja']['abierta'] = 1;
            $this->data['Caja']['transacciones'] = 0;
			if ($this->Caja->save($this->data)) {                
                $this->User->read(null, $this->data['Caja']['user_id']);
                $this->User->saveField('activo', "0");
				$this->Session->setFlash(__('La caja ha sido creada ', true));
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The caja could not be saved. Please, try again.', true));
			}
		}
		$users = $this->User->find('list',array('conditions' => array('User.activo' => '1')));
        $recaudaciones = $this->Caja->Recaudacione->find('list');
		$this->set(compact('users'));
        $this->set(compact('recaudaciones'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid caja', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Caja->save($this->data)) {
				$this->Session->setFlash(__('The caja has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The caja could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Caja->read(null, $id);
		}
		$users = $this->Caja->User->find('list');
		$this->set(compact('users'));
	}
	function cerrar($id = null){
        //print_r($data);
        $this->Caja->read(null, $id);
		$this->Caja->saveField('abierta', '0' );
        $this->User->read(null, $this->Auth->user('id'));
        $this->User->saveField('activo', "0");
        $this->Session->setFlash(__('Caja cerrada', true));
        $this->redirect(array('controller' =>'users','action' => 'perfil'));
	}
	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for caja', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Caja->read(null, $id)) {
            $this->Caja->saveField('abierta',0);
			$this->Session->setFlash(__('Caja deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Caja was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
		function beforeFilter() {
		//$this->Acl->allow($aroAlias, $acoAlias);
   		 parent::beforeFilter();
    		    	
			$this->Auth->allow(array('*'));

    	}
}
